#include "ElaWidget.h"
#include "ElaDialog.h"
#include <QApplication>
#include "ElaApplication.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QListWidget>
#include <QMessageBox>
#include <QGroupBox>
#include <QLineEdit>
#include <QTimer>


// 设置对话框
class SettingsDialog : public ElaDialog
{
    Q_OBJECT
public:
    SettingsDialog(QWidget* parent = nullptr) : ElaDialog(parent)
    {
        setWindowTitle("设置");
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

        resize(350, 250);
        
        auto* layout = new QVBoxLayout(this);
        
        // 主题设置组
        auto* themeGroup = new QGroupBox("主题设置");
        auto* themeLayout = new QVBoxLayout(themeGroup);
        
        auto* darkThemeBtn = new QPushButton("深色主题");
        auto* lightThemeBtn = new QPushButton("浅色主题");
        auto* autoThemeBtn = new QPushButton("自动主题");
        
        themeLayout->addWidget(darkThemeBtn);
        themeLayout->addWidget(lightThemeBtn);
        themeLayout->addWidget(autoThemeBtn);
        
        // 窗口设置组
        auto* windowGroup = new QGroupBox("窗口设置");
        auto* windowLayout = new QVBoxLayout(windowGroup);
        
        auto* stayTopCheck = new QPushButton("切换置顶状态");
        auto* fixedSizeCheck = new QPushButton("切换固定大小");
        
        windowLayout->addWidget(stayTopCheck);
        windowLayout->addWidget(fixedSizeCheck);
        
        layout->addWidget(themeGroup);
        layout->addWidget(windowGroup);
        layout->addStretch();
        
        // 连接信号
        connect(stayTopCheck, &QPushButton::clicked, this, [this]() {
            setIsStayTop(!getIsStayTop());
            QMessageBox::information(this, "提示", 
                getIsStayTop() ? "窗口已置顶" : "窗口取消置顶");
        });
        
        connect(fixedSizeCheck, &QPushButton::clicked, this, [this]() {
            setIsFixedSize(!getIsFixedSize());
            QMessageBox::information(this, "提示",
                getIsFixedSize() ? "窗口大小已固定" : "窗口大小可调整");
        });
        
        connect(this, &SettingsDialog::closeButtonClicked, this, &SettingsDialog::close);
    }
};

// 关于对话框
class AboutDialog : public ElaDialog
{
    Q_OBJECT
public:
    AboutDialog(QWidget* parent = nullptr) : ElaDialog(parent)
    {
        setWindowTitle("关于");
        setWindowButtonFlags(ElaAppBarType::CloseButtonHint);
        setFixedSize(300, 200);
        
        auto* layout = new QVBoxLayout(this);
        
        auto* titleLabel = new QLabel("Ela 示例应用");
        titleLabel->setAlignment(Qt::AlignCenter);
        titleLabel->setStyleSheet("font-size: 18px; font-weight: bold; color: #2c3e50;");
        
        auto* versionLabel = new QLabel("版本 1.0.0");
        versionLabel->setAlignment(Qt::AlignCenter);
        
        auto* descLabel = new QLabel("这是一个展示 ElaWidget 和 ElaDialog\n结合使用的示例应用程序");
        descLabel->setAlignment(Qt::AlignCenter);
        
        auto* copyrightLabel = new QLabel("© 2024 Ela Framework");
        copyrightLabel->setAlignment(Qt::AlignCenter);
        copyrightLabel->setStyleSheet("color: #7f8c8d; margin-top: 20px;");
        
        layout->addStretch();
        layout->addWidget(titleLabel);
        layout->addWidget(versionLabel);
        layout->addWidget(descLabel);
        layout->addStretch();
        layout->addWidget(copyrightLabel);
        
        connect(this, &AboutDialog::closeButtonClicked, this, &AboutDialog::close);
    }
};

// 主窗口
class MainWindow : public ElaWidget
{
    Q_OBJECT
public:
    MainWindow(QWidget* parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("Ela 示例应用 - 主窗口");
        resize(600, 500);
        
        // 设置完整的窗口按钮
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint |
                            ElaAppBarType::MinimizeButtonHint |
                            ElaAppBarType::MaximizeButtonHint |
                            ElaAppBarType::CloseButtonHint);
        
        setupUI();
        setupConnections();
        
        // 居中显示
        QTimer::singleShot(100, this, &MainWindow::moveToCenter);
    }

private:
    QListWidget* m_taskList;
    QPushButton* m_addButton;
    QPushButton* m_settingsButton;
    QPushButton* m_aboutButton;
    
    void setupUI()
    {
        auto* mainLayout = new QVBoxLayout(this);
        
        // 标题
        auto* titleLabel = new QLabel("任务管理器");
        titleLabel->setStyleSheet("font-size: 24px; font-weight: bold; color: #34495e; margin: 20px;");
        titleLabel->setAlignment(Qt::AlignCenter);
        
        // 任务列表
        m_taskList = new QListWidget();
        m_taskList->addItems({"示例任务 1", "示例任务 2", "示例任务 3"});
        
        // 按钮区域
        auto* buttonLayout = new QHBoxLayout();
        m_addButton = new QPushButton("添加任务");
        m_settingsButton = new QPushButton("设置");
        m_aboutButton = new QPushButton("关于");
        
        buttonLayout->addWidget(m_addButton);
        buttonLayout->addStretch();
        buttonLayout->addWidget(m_settingsButton);
        buttonLayout->addWidget(m_aboutButton);
        
        mainLayout->addWidget(titleLabel);
        mainLayout->addWidget(m_taskList, 1);
        mainLayout->addLayout(buttonLayout);
    }
    
    void setupConnections()
    {
        // 连接按钮信号
        connect(m_addButton, &QPushButton::clicked, this, &MainWindow::showAddTaskDialog);
        connect(m_settingsButton, &QPushButton::clicked, this, &MainWindow::showSettingsDialog);
        connect(m_aboutButton, &QPushButton::clicked, this, &MainWindow::showAboutDialog);
        
        // 连接标题栏信号
        connect(this, &MainWindow::closeButtonClicked, this, &MainWindow::close);
        connect(this, &MainWindow::themeChangeButtonClicked, this, []() {
            QMessageBox::information(nullptr, "主题切换", "主题切换按钮被点击");
        });
    }
    
private Q_SLOTS:
    void showAddTaskDialog()
    {
        ElaDialog dialog(this);
        dialog.setWindowTitle("添加新任务");
        dialog.setWindowButtonFlags(ElaAppBarType::CloseButtonHint);
        dialog.resize(300, 150);
        
        auto* layout = new QVBoxLayout(&dialog);
        
        auto* label = new QLabel("输入新任务内容:");
        auto* lineEdit = new QLineEdit();
        auto* buttonLayout = new QHBoxLayout();
        auto* okButton = new QPushButton("确定");
        auto* cancelButton = new QPushButton("取消");
        
        buttonLayout->addWidget(okButton);
        buttonLayout->addWidget(cancelButton);
        
        layout->addWidget(label);
        layout->addWidget(lineEdit);
        layout->addLayout(buttonLayout);
        
        // 连接对话框按钮
        connect(okButton, &QPushButton::clicked, &dialog, [&]() {
            if (!lineEdit->text().isEmpty()) {
                m_taskList->addItem(lineEdit->text());
                dialog.close();
            }
        });
        
        connect(cancelButton, &QPushButton::clicked, &dialog, &ElaDialog::close);
        connect(&dialog, &ElaDialog::closeButtonClicked, &dialog, &ElaDialog::close);
        
        dialog.exec();
    }
    
    void showSettingsDialog()
    {
        auto* dialog = new SettingsDialog(this);
        dialog->show();
        dialog->moveToCenter();
    }
    
    void showAboutDialog()
    {
        auto* dialog = new AboutDialog(this);
        dialog->show();
        dialog->moveToCenter();
    }
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);  // 启用高 DPI (界面布局和几何)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);                                               //  启用自动高 DPI 缩放 (图标和图片)
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);  // 使用精确的缩放因子，不进行四舍五入
#else
    // 根据实际屏幕缩放比例更改
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);

    eApp->init();  // 初始化资源、字体等
    // eApp->setWindowDisplayMode(ElaApplicationType::ElaMica);
    
    MainWindow window;
    window.show();
    
    return app.exec();
}

#include "main_window_with_dialogs.moc"